using System;
using System.Data;
using System.Configuration;

using System.Collections;
using System.Web;
using System.Web.Security;

using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
using Component;
using System.IO;


public partial class BackEnd_NewsModify : System.Web.UI.UserControl
{
    string action = "";
    protected string onclick = "";
    public string nameImage = "";
    News news = null;
    public string _Url = BackEndQuery._AdminQuery+ConstString.NewL;
    public string _AU = "";
    public int idNew = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.rfvtxtNoiDungTomTatEL.ErrorMessage = ConstString._NhapNoiDung;
        this.rfvtxtTitileEL.ErrorMessage = ConstString._NhapNoiDung;
        this.rfvtxtTitileVN.ErrorMessage = ConstString._NhapNoiDung;
        this.rfvtxtNoiDungTomTatVN.ErrorMessage = ConstString._NhapNoiDung;

        action = QueryHelper.GetQueryString(Request, ConstString._Action);
        if (action == ConstString.Update)
        {
            idNew = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._id));
            news = NewsDB.GetObjectByid(idNew,true);
            txtTitileVN.Text = news.Titile;
            txtNoiDungTomTatVN.Text = news.Summary;
            txtNoiDungTomTatEL.Text = news.SummaryEL;
            txtContentEL.Value = news.ContentEL;
            txtContentVN.Value = news.Content;
            nameImage = news.Img;
            _AU += ConstString.AUCapNhat+ " "+news.Titile;
        }
        this.onclick = @"OpenWindowUploadFile(""FileManagement/File.aspx?" + ConstString.NewM + "=" +idNew.ToString () + "&id=" + this.txtImage.ClientID + @"&idimg=" + this.imageUpload.ClientID + @""",""window"",""width = 630px, height =520px"");";
    }
    public void cmdLuu_Click(object sender, System.EventArgs e)
    {
        string fileNameImage = this.txtImage.Value;
        if (action == ConstString.Update)
        {
            news.Titile = txtTitileVN.Text;
            news.TitileEL = txtTitileEL.Text;
            if (fileNameImage.CompareTo("") != 0)
                news.Img = fileNameImage;
            else
                news.Img = nameImage;
            news.Summary = txtNoiDungTomTatVN.Text;
            news.SummaryEL = txtNoiDungTomTatEL.Text;
            news.Content= txtContentVN.Value;
            news.ContentEL = txtContentEL.Value;
            news.Date = DateTime.Now.Date;
            news.New = true;
            if (NewsDB.Save(news) == 1)
            {
                if (fileNameImage.CompareTo("") != 0)
                    File.Delete(Server.MapPath(nameImage.Replace("/", "\\")));
                this.lbMessage.Text = ConstString.capNhatTC;                
            }
            else this.lbMessage.Text = ConstString.capNhatTB;
        }
        if (action == ConstString.Add)
        {
            News new2 = new News(KetNoiCSDL.session);
            new2.Titile = txtTitileVN.Text;
            new2.TitileEL = txtTitileEL.Text;
            new2.Date = DateTime.Now.Date;
            new2.Summary = txtNoiDungTomTatVN.Text;
            new2.SummaryEL = txtNoiDungTomTatEL.Text;
            new2.Content = txtContentVN.Value;
            new2.ContentEL = txtContentEL.Value;
            new2.Active = true;
            new2.New = true;
            new2.Date = DateTime.Now.Date;           
            if (NewsDB.Save(new2) == 1)
            {
                Sessions.Add ="y";
                new2.UrlDetailBackend = BackEndQuery._AdminQuery + ConstString.NewM + "&" + ConstString._Action + "=" + ConstString.Update + "&"+ConstString._id + "=" + new2.Id.ToString();;
                new2.UrlDetailFronend = ConstString._Default + ConstString._DetailNew + "&" + ConstString._id + "=" + new2.Id.ToString();
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath(ConstString._PathFileNewImg));
                if (dir.Exists)
                {
                    FileInfo[] alFile = dir.GetFiles();
                    if (alFile != null && alFile.Length > 0)
                    {
                        string directory1 = ConstString._PathFileNewImg + "//" + new2.Id.ToString();
                        if (Directory.Exists(Server.MapPath(directory1.Replace("//", "\\")))==false)
                            Directory.CreateDirectory(Server.MapPath(directory1.Replace("//", "\\")));
                        new2.Img = directory1.Replace("//", "/") + "/" + alFile[0].Name;
                        alFile[0].MoveTo(Server.MapPath(new2.Img.Replace("//", "/").Replace("/", "\\")));
                        try
                        {
                            File.Delete(Server.MapPath(fileNameImage.Replace("/", "\\")));
                        }
                        catch { }
                    }
                }
                new2.Save();
               this.lbMessage.Text = ConstString.chenTC;
               Response.Redirect(new2.UrlDetailBackend + new2.Id.ToString());
                GanNullString();
            }
            else this.lbMessage.Text = ConstString.chenTB;
        }
    }
    void GanNullString()
    {
        txtTitileVN.Text = "";
        txtTitileEL.Text = "";
        txtNoiDungTomTatVN.Text = "";
        txtNoiDungTomTatEL.Text ="";
        txtContentEL.Value = "";
        txtContentVN.Value ="";
    }
  
}
 